package com.baidu.mapsdkexample.geometry.markercluster;

import android.graphics.Bitmap;

import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapsdkexample.util.clusterutil.clustering.ClusterItem;

import java.io.File;
import java.io.FileOutputStream;

import toy.keli.picmap.R;
import toy.keli.picmap.model.Model;
import toy.keli.picmap.model.def;
import toy.keli.picmap.model.pic;

/**
 * 每个Marker点，包含Marker点坐标以及图标
 */
public class MyItem implements ClusterItem {
    public final LatLng mPosition;
    public def data;
    public int id;

    public MyItem(LatLng latLng) {
        mPosition = latLng;
    }
    
    public MyItem(LatLng latLng, def d) {
        mPosition = latLng;
        this.data=d;
        this.id=d.get("id").toInt();
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    @Override
    public BitmapDescriptor getBitmapDescriptor() {
        return BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
    }

    public def getData() {
        return data;
    }

    //缩略图
    public String getThumbnail(int w,int h) {
        pic p=new pic(this.id);
        Bitmap bimg = p.thumbnail(w, h);
        String newFile = "/mnt/sdcard/picmap/tmp/"+p.getFileName()+".jpg";

        File file = new File(newFile);
        try {
            file.createNewFile();
            FileOutputStream outputStream = new FileOutputStream(file);
            bimg.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            p.copyExifTo(newFile);
            bimg.recycle();
        }catch (Exception e){
            e.printStackTrace();
        }

        return newFile;
    }

    public pic getPic(){
        return (pic)Model.populateRecord(pic.class,data);
    }
}
